/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-05-21 10:55:55
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-05-21 11:27:52
 * @FilePath: \newMould_BHZ\src\components\HntManage\EquipmentCalibration\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import publicForm from "@/components/public/up-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveMachineCheck, //保存
    getMachineCheckByFid, //详情

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "typ"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "",
                userId: "",
                userName: "",
                fProjectname: "",
                fProjectid: "",
                fCompanyname: "",
                fCompanyid: "",
                fStationid: "", //拌合站编号
                fStationname: "", //拌合站
                fMachineid: "", //拌合机编号
                fMachinename: "", //拌合机
                fCertificatenumber: "", //检定证书编号
                fCheckresult: "", //检定校准结果
                fNextcheckdates: "", //下次检定时间
                fScanfile: "", //证书扫描件资料id
                fMaintainerid: "", //维护人id
                fMaintainername: "", //维护人
                fMaintenancedates: this.common.day(0), //维护时间
                recordfiles: [], // 删除文件id集合
            },
            formObj: formJs.formObj,
            fSourcefileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        typ: {
            handler(newVal) {
                if (newVal) {
                    this.typs = newVal;
                    if (newVal == 1) {
                        this.formObj[0].label = "生产厂家"
                    } else if (newVal == 2) {
                        this.formObj[0].label = "供应商"

                    }
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[5].uploadSelect = true;
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[5].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fMaterialname") {
                this.formData.fMaterialid = data.value;
                this.formData.fMaterialname = data.label;
                this.formData.fFrequency = data.fFrequency;
                this.formData.fStandard = data.fStandard;
                this.formData.fUnit = data.fUnit;
                this.formData.fNote = data.fNote;
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 刪除
        delteFile(file) {
            for (let index = 0; index < this.fSourcefileList.length; index++) {
                if (file.uid == this.fSourcefileList[index].uid) {
                    if (this.fSourcefileList[index].atta == undefined) {
                        this.recordfiles.push({
                            fId: this.fSourcefileList[index].fId,
                        });
                    } else {
                        this.recordfiles.push({
                            fId: this.fSourcefileList[index].atta.fId,
                        });
                    }
                    this.fSourcefileList.splice(index, 1);
                }
            }
        },

        // 详情
        getDetail(obj) {
            this.loading = true;
            getMachineCheckByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.machineCheck;
                    this.formData = data;
                    this.formData.fMaintainername = this.userInfo.realName;
                    this.formData.fMaintainerid = this.userInfo.userId;
                    this.loading = false;

                    if (this.formData.fMaintenancedates == null) {
                        this.formData.fMaintenancedates = this.common.day(0);
                    }

                    if (this.formData.fCertificatenumber == null) {
                        this.formData.fCertificatenumber = "";
                    }
                    if (this.formData.fCheckresult == null) {
                        this.formData.fCheckresult = "";
                    }
                    if (this.formData.fNextcheckdates == null) {
                        this.formData.fNextcheckdates = "";
                    }
                    this.fSourcefileList = res.data.scanFileList; //证书扫描件
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm(status) {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fProjectname = this.userInfo.projectFullName; //当前登录人id
            this.formData.fProjectid = this.userInfo.projectId; //当前登录人名称
            this.formData.fCompanyname = this.userInfo.companyName; //当前登录人id
            this.formData.fCompanyid = this.userInfo.companyId; //当前登录人名称

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;

            this.isLock = true
            saveMachineCheck(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};